﻿using PivalSoft.LDMS.Data;
using PivalSoft.LDMS.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PivalSoft.LDMS
{
    public partial class frmAddCourt : Form
    {
        public int UserId = 0;
        public int courtId = 0;

        public frmAddCourt()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateCourt())
            {
                LDMSRepository repository = new LDMSRepository();
                Court court = new Court();
                court.CourtName = txtCourtname.Text.Trim();
                court.City = txtCity.Text.Trim();
                court.StateID = Convert.ToInt32(cbmState.SelectedValue);
                court.ZipCode = txtZipCode.Text.Trim();
                court.IsActive = true;
                court.CreatedBy = UserId;
                court.CreatedDate = DateTime.Now;
                court.CourtID = courtId;
                repository.AddCourt(court);
                MessageBox.Show("Court details has been saved successfully", Resources.MessageCaption);
                ResetControl();
            }
        }

        private void ResetControl()
        {
            courtId = 0;
            txtCourtname.Text = string.Empty;
            txtCity.Text = string.Empty;
            cbmState.SelectedIndex = 0;
            txtZipCode.Text = string.Empty;
        }

        private bool ValidateCourt()
        {
            bool isValid = true;
            if (string.IsNullOrEmpty(txtCourtname.Text))
            {
                MessageBox.Show(Resources.RequireCourtName, Resources.MessageCaption);
                isValid = false;
            }

            if (string.IsNullOrEmpty(txtCity.Text))
            {
                MessageBox.Show(Resources.RequireCity, Resources.MessageCaption);
                isValid = false;
            }

            if (cbmState.SelectedIndex == 0)
            {
                MessageBox.Show(Resources.RequireState, Resources.MessageCaption);
                isValid = false;
            }

            return isValid;

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            SearchCourt();
        }

        private void SearchCourt()
        {
            List<KeyValuePair<string, string>> parameters = new List<KeyValuePair<string, string>>();
            if (!string.IsNullOrEmpty(txtSearchCity.Text.Trim()))
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.CITY, txtSearchCity.Text));
            }
            if (!string.IsNullOrEmpty(txtSearchCourt.Text.Trim()))
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.NAME, txtSearchCourt.Text));
            }
            if (!string.IsNullOrEmpty(txtSearchZip.Text.Trim()))
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.ZIP, txtSearchZip.Text));
            }
            if (cmbSearchState.SelectedIndex > 0)
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.STATE, cmbSearchState.SelectedValue.ToString()));
            }

            if (parameters.Count > 0)
            {
                LDMSRepository repository = new LDMSRepository();
                var courts = repository.SearchCourt(parameters);
                dgCourt.DataSource = courts;
            }
            else { MessageBox.Show(Resources.NoneFilter, Resources.MessageCaption); }
        }

        private void frmAddCourt_Load(object sender, EventArgs e)
        {
            dgCourt.AutoGenerateColumns = false;

            LDMSRepository repository = new LDMSRepository();
            var state = repository.GetTypeMasters("state");
            state.Insert(0, new TypeMaster { TypeMasterID = 0, TypeName = "Select State" });

            cbmState.DataSource = state;
            cbmState.ValueMember = "TypeMasterID";
            cbmState.DisplayMember = "TypeName";

            cmbSearchState.DataSource = state;
            cmbSearchState.ValueMember = "TypeMasterID";
            cmbSearchState.DisplayMember = "TypeName";
        }

        private void dgCourt_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (Convert.ToString(dgCourt.Rows[e.RowIndex].Cells[e.ColumnIndex].Value).Equals("Edit"))
            {
                txtCourtname.Text = Convert.ToString(dgCourt.Rows[e.RowIndex].Cells["CourtName"].Value);
                txtCity.Text = Convert.ToString(dgCourt.Rows[e.RowIndex].Cells["City"].Value);
                cbmState.SelectedIndex = cbmState.Items.IndexOf(cbmState.Items.Cast<TypeMaster>().FirstOrDefault(s =>
                    s.TypeMasterID == Convert.ToInt32(dgCourt.Rows[e.RowIndex].Cells["StateID"].Value)));
                txtZipCode.Text = Convert.ToString(dgCourt.Rows[e.RowIndex].Cells["ZipCode"].Value);
                courtId = Convert.ToInt32(dgCourt.Rows[e.RowIndex].Cells["ColCourtID"].Value);
                dgCourt.DataSource = null;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
